﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using NStl.Debugging;
using NStl.Query;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace NStl.NUnit.Support.SupportTests
{
    [TestFixture]
    public class TestSanityFixture
    {
        [Test]
        public void AllTestFixturesArePublicClasses()
        {
            foreach (Type tp in GetType().Assembly.GetTypes())
            {
                if (tp.GetCustomAttributes(typeof(TestFixtureAttribute), true).Length == 0)
                    continue;
                Assert.That(tp.IsPublic, "{0} is not public", tp.FullName);
                Assert.That(!tp.IsAbstract, "{0} is is abstract", tp.FullName);
            }
        }

        [Test]
        public void AllSourceFilesHaveCorrectCopyright()
        {
            string copyright = @"#region Copyright (c) 2003 - 2008, Andreas Mueller";

            //"file:///M:/CodePlex/nstl/nstl/bin/nstl.nunit.DLL"
            string codebase = GetType().Assembly.CodeBase.Replace("file:///", "").Replace("/", "\\");
            string dir = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(codebase), @"..\source"));
            Assert.That(Directory.Exists(dir), Is.True);

            Func<string, bool> hasCopyright = delegate(string csFile)
            {
                using (StreamReader f = new StreamReader(csFile, true))
                {
                    string s = f.ReadLine();
                    return !s.StartsWith(copyright);
                }
            };
            Func<string, bool> isNotResource = delegate(string csFile)
            {
                return !csFile.EndsWith("Resource.Designer.cs", StringComparison.OrdinalIgnoreCase);
            };
            List<string> failed = new List<string>(Sequence.Filter(Sequence.Filter(Directory.GetFiles(dir, "*.cs", SearchOption.AllDirectories), isNotResource), hasCopyright));

            if (failed.Count != 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string s in failed)
                    sb.AppendLine(s);
                Assert.That(false, "Missing/Bad Copyright:{0}{1}", Environment.NewLine, sb);
            }

        }

        [Test]
        public void AllCollectionsHaveDebuggerAttributes()
        {
            foreach (Type tp in typeof(CollectionDebuggerProxy).Assembly.GetTypes())
            {
                if (tp.Namespace != "NStl.Collections")
                    continue;

                if (!tp.IsPublic)
                    continue;

                if (tp.IsAbstract)
                    continue;

                if (tp.DeclaringType != null)
                    continue;

                object[] dbgDispay = tp.GetCustomAttributes(typeof(DebuggerDisplayAttribute), true);
                Assert.That(dbgDispay, Has.Length(1), "{0} does not have the DebuggerDisplayAttribute attached", tp.FullName);
                Assert.That(((DebuggerDisplayAttribute)dbgDispay[0]).Value, Is.EqualTo("Dimension:[{Count}]"), "{0} 's DebuggerDisplayAttribute does not have the correct content", tp.FullName);

                object[] dbgProxy = tp.GetCustomAttributes(typeof(DebuggerTypeProxyAttribute), true);
                Assert.That(dbgProxy, Has.Length(1), "{0} does not have the DebuggerTypeProxyAttribute attached", tp.FullName);
                Assert.That(((DebuggerTypeProxyAttribute)dbgProxy[0]).ProxyTypeName, Is.EqualTo(typeof(CollectionDebuggerProxy).AssemblyQualifiedName), "{0} 's DebuggerTypeProxyAttribute does not point to CollectionDebuggerProxy", tp.FullName);

            }
        }
    }
}
